package com.example.sherlockzhong.plush_v2.View;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.sherlockzhong.plush_v2.MainActivity;
import com.example.sherlockzhong.plush_v2.R;

import org.json.JSONObject;

/*
 * Created by DosonLeungs on 2014/9/6.
 */

public class BasicCardView extends HorizontalScrollView {

    protected MainActivity app = null;
    protected Context ctx;
    protected String cardId = null;
    protected String cardClass = null;
    protected int scrollDistance = 0;
    protected MainContentFrame rootConatiner;
    private BasicCardView self = this;

    public BasicCardView(Context context) {
        super(context);
        this.ctx = context;

    }

    public BasicCardView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.ctx = context;

    }

    public Context getCtx() {
        return ctx;
    }

    public void setCtx(Context ctx) {
        this.ctx = ctx;
    }

    public MainActivity getApp() {
        return app;
    }

    public void setApp(MainActivity app) {
        this.app = app;
    }

    public int getScrollDistance() {
        return scrollDistance;
    }

    public void setScrollDistance(int scrollDistance) {
        this.scrollDistance = scrollDistance;
    }

    public String getCardId() {
        return cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    public String getCardClass() {
        return cardClass;
    }

    public void setCardClass(String cardClass) {
        this.cardClass = cardClass;
    }

    public MainContentFrame getRootConatiner() {
        return rootConatiner;
    }

    public void setRootConatiner(MainContentFrame rootConatiner) {
        this.rootConatiner = rootConatiner;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        cardMotionEvent(ev);
        return super.onTouchEvent(ev);
    }

    protected void cardMotionEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_UP:
                detectScroll();
                break;
            case MotionEvent.ACTION_MOVE:
                fadeBackground(ev);
                break;
            case MotionEvent.ACTION_CANCEL:
                break;
            default:
                break;
        }
    }

    protected void scrollBack() {
        final BasicCardView self = this;
        this.post(new Runnable() {
            @Override
            public void run() {
                self.smoothScrollTo(self.scrollDistance, 0);
                self.setAlpha(1);
            }
        });
    }

    private void fadeBackground(MotionEvent ev){
        double nowX = (double)getScrollX();
        double space = Math.abs(scrollDistance - nowX);
        double precent = space/(scrollDistance);
        this.setAlpha((float)((1/(precent-1.3))+1.74));
        //Log.e("Distance = "+scrollDistance,"precent = "+precent);
    }

    protected void detectScroll() {
        int scrollX = getScrollX();
        if (scrollX - this.scrollDistance == 0) {
            if (getCtx() != null && this.app != null)
                this.app.controller.showCardInner(rootConatiner.getCardIndex(self));
        } else if (scrollX < this.scrollDistance * 2 / 3) {
            flyOut(0);
        } else if (scrollX > this.scrollDistance * 4 / 3) {
            flyOut(1);
        } else {
            scrollBack();
        }
    }

    /**
     * remove
     *
     * @param direction
     */
    protected void flyOut(int direction) {
        final int _direction = direction;
        final BasicCardView self = this;
        this.post(new Runnable() {
            @Override
            public void run() {
                if (_direction == 0) {
                    int cardIndex = rootConatiner.getCardIndex(self);
                    try {
                        JSONObject cardData = (JSONObject) app.controller.allData.get(cardIndex);
                        if (cardData.getString("card_type").equalsIgnoreCase("organization") || cardData.getString("card_type").equalsIgnoreCase("myclass") || cardData.getString("card_type").equalsIgnoreCase("tutor") || cardData.getString("card_type").equalsIgnoreCase("official")) {
                            self.smoothScrollTo(0, 0);
                        } else {
                            scrollBack();
                            Toast.makeText(self.getContext(), "这类卡片不能收藏", Toast.LENGTH_SHORT).show();
                            return;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    self.setBackgroundResource(R.drawable.transparent);
                    self.smoothScrollTo(self.scrollDistance * 2, 0);
                }
                self.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        int index = rootConatiner.removeView(self);
                        if (_direction == 0) {
                            app.controller.collectCardData(index);
                        } else {
                            app.controller.showRemoveToast();
                        }
                        app.controller.removeCardData(index);
                    }
                }, 500);
            }
        });
    }

    protected void showInnerTable(int index) {
    }

    ;

}
